/**
 * 初始化联系人详情对话框
 */
var ContactInfoDlg = {
    contactInfoData : {},
    zTreeInstance : null,
    validateFields: {
        name: {
            validators: {
                notEmpty: {
                    message: '用户名不能为空'
                }
            }
        },
        customerCompanyName: {
            validators: {
                notEmpty: {
                    message: '企业客户必选'
                }
            }
        },
        post: {
            validators: {
                notEmpty: {
                    message: '岗位职务不能为空'
                }
            }
        },
        email: {
            validators: {
                emailAddress: {
                    message: '输入正确的邮箱格式'
                }
            }
        },
        mobileNum: {
            validators: {
                notEmpty: {
                    message: '手机号不能为空'
                },
                digits:{
                    message: '必须为数字'
                },
                stringLength: {
                    min: 11,
                    max: 11,
                    message: '请输入11个数字'
                }
            }
        }

    }
};

/**
 * 清除数据
 */
ContactInfoDlg.clearData = function() {
    this.contactInfoData = {};
}

/**
 * 设置对话框中的数据
 *
 * @param key 数据的名称
 * @param val 数据的具体值
 */
ContactInfoDlg.set = function(key, val) {
    this.contactInfoData[key] = (typeof val == "undefined") ? $("#" + key).val() : val;
    return this;
}

/**
 * 设置对话框中的数据
 *
 * @param key 数据的名称
 * @param val 数据的具体值
 */
ContactInfoDlg.get = function(key) {
    return $("#" + key).val();
}

/**
 * 关闭此对话框
 */
ContactInfoDlg.close = function() {
    parent.layer.close(window.parent.Contact.layerIndex);
}

/**
 * 收集数据
 */
ContactInfoDlg.collectData = function() {
    this
    .set('id')
    .set('name')
    .set('customerCompanyId')
    .set('post')
    .set('mobileNum')
    .set('standbyMobileNum')
    .set('landline')
    .set('facsimile')
    .set('email')
    .set('wechat')
    .set('qq')
    .set('attitude')
    .set('remark')
        .set('departName')
        .set('customerCompanyName')
    ;
}
/**
 * 验证数据是否为空
 * @returns {*|jQuery}
 */
ContactInfoDlg.validate = function () {
    $('#content').data("bootstrapValidator").resetForm();
    $('#content').bootstrapValidator('validate');
    return $("#content").data('bootstrapValidator').isValid();
}
/**
 * 提交添加
 */
ContactInfoDlg.addSubmit = function() {

    this.clearData();
    this.collectData();
    if(!this.validate()){
        return;
    }
    //提交信息
    var ajax = new $ax(Feng.ctxPath + "/contact/add", function(data){
        Feng.success("添加成功!");
        window.parent.Contact.table.refresh();
        ContactInfoDlg.close();
    },function(data){
        Feng.error("添加失败!" + data.responseJSON.message + "!");
    });
    ajax.set(this.contactInfoData);
    ajax.start();
}

/**
 * 提交修改
 */
ContactInfoDlg.editSubmit = function() {

    this.clearData();
    this.collectData();
    if(!this.validate()){
        return;

    }
    //提交信息
    var ajax = new $ax(Feng.ctxPath + "/contact/update", function(data){
        Feng.success("修改成功!");
        window.parent.Contact.table.refresh();
        ContactInfoDlg.close();
    },function(data){
        Feng.error("修改失败!" + data.responseJSON.message + "!");
    });
    ajax.set(this.contactInfoData);
    ajax.start();
}

ContactInfoDlg.showCustomerCompanySelectTree = function () {
    Feng.showInputTree("customerCompanyName", "customerCompanyTreeDiv", 15, 34);
};

ContactInfoDlg.onClickCustomerCompany = function (e, treeId, treeNode) {
    $("#customerCompanyName").attr("value", treeNode.name);
    $("#customerCompanyId").attr("value", treeNode.id);
    $("#customerCompanyTreeDiv").fadeOut("fast");
}


$(function() {
    Feng.initValidator("content",ContactInfoDlg.validateFields);
    var customerCompanyTree = new $ZTree("customerCompanyTree", "/customerCompany/tree/all");
    customerCompanyTree.bindOnClick(ContactInfoDlg.onClickCustomerCompany);
    customerCompanyTree.init();
});
